%% Further analysis of FRAP data
close all; 
% Need FRAP_struct_corrected.mat after full analysis

%% Load data
% Directory name
% dirname = "/Users/bknapp/Documents/FRAP_Data/221102_mitotracker_FRAP/bw25113_pad_2/matlab_structs/";
% dirname = "/Users/bknapp/Documents/FRAP_Data/221102_mitotracker_FRAP/fadR/matlab_structs/";
% dirname = "/Users/bknapp/Documents/FRAP_Data/20221103_mitotracker_FRAP/BW_LB_triclosan5ngmL_37C/matlab_structs/";
% dirname = "/Users/bknapp/Documents/FRAP_Data/20221103_mitotracker_FRAP/BW_LB_27to37C/matlab_structs/";
% dirname = "/Users/bknapp/Documents/FRAP_Data/20221103_mitotracker_FRAP/BW25113_LB_37C/matlab_structs/";
% dirname = "/Users/bknapp/Documents/FRAP_Data/20221103_mitotracker_FRAP/MG1655_LB_37C/matlab_structs/";
% Matlab file to load
fname = "FRAP_struct_corrected.mat"; 

load(strcat(dirname,fname));

% Rename for analysis
tempstruct = FRAP_struct_corrected;

%% Plot binned data
Ntraces = length(tempstruct);

time_recovery_total = [];
Fcap_norm_total = [];
for k=1:Ntraces
    time_recovery = FRAP_struct_corrected(k).time_recovery; % Recovery time vector (in seconds)
    Fcap = FRAP_struct_corrected(k).cap_fluor_recovery_corrected; % Fluorescence of cap region (AU)
    Fcap_eq = FRAP_struct_corrected(k).cap_final_fluor; % Equilibrated/final fluorescence
    Fcap_norm = (Fcap - Fcap(1))/(Fcap_eq - Fcap(1)); % Normalized FRAP recovery
    
    time_recovery_total = [time_recovery_total; time_recovery];
    Fcap_norm_total = [Fcap_norm_total; Fcap_norm];
    
end

% Define bin window
gx = (0:0.5:max(time_recovery_total))';
% gx = (0:0.5:9);
[b,n,s] = bindata(time_recovery_total, Fcap_norm_total, gx);

figure;
shadedErrorBar(gx,b,s,'lineprops', {'-k', 'linewidth', 2});
set(gca, 'fontsize', 20);
xlabel('Time (s)');
ylabel('FRAP recovery');
set(gcf, 'position', [0 0 400 300]);
ylim([0 1.2]);
xlim([0 10]);

%% Plot correlations of viscosity vs. (initial cell fluor.) (initial time) (cap size) (cell size) (cap proportion of cell) (initial fluor. concentration)
Ncorr = 6;
eta_all = [tempstruct.eta];
eta_all_err = [tempstruct.eta_err];

cell_fluor_ini = [];
time_ini = [];
cap_area = [];
cell_area = [];
cap_proportion = [];

for k=1:Ntraces
    
    cell_fluor_ini = [cell_fluor_ini; tempstruct(k).cell_fluor_tot(1)];
    time_ini = [time_ini; tempstruct(k).timestamp(1)];
    cap_area = [cap_area; tempstruct(k).cap_area(1)];
    cell_area = [cell_area; tempstruct(k).cell_area(1)];
    cap_proportion = [cap_proportion; tempstruct(k).cap_area(1)/tempstruct(k).cell_area(1)];
end

fluor_cell_conc_ini = cell_fluor_ini./cell_area;
time_ini = (time_ini - time_ini(1))/60; % Measurement timestamp (in minutes)

figure;
% Plot viscosity vs. initial cell fluorescence
subplot(2,3,1);
errorbar(cell_fluor_ini, eta_all, eta_all_err, 'ko');
xlabel('Total cell fluorescence');
ylabel('Viscosity (P)');
ylim([0 15]);

% Plot viscosity vs. initial time
[time_ini_sort, I] = sort(time_ini);
eta_all_sort = eta_all(I);
eta_all_err_sort = eta_all_err(I);
subplot(2,3,2);
errorbar(time_ini_sort-time_ini_sort(1), eta_all_sort, eta_all_err_sort, 'ko');
xlabel('FRAP timestamp (min)');
ylabel('Viscosity (P)');
ylim([0 15]);

% Plot viscosity vs. Cap size (bleached area size, 2D)
subplot(2,3,3);
errorbar(cap_area, eta_all, eta_all_err, 'ko');
xlabel('Bleached cap area (\mum^2)');
ylabel('Viscosity (P)');
ylim([0 15]);

% Plot viscosity vs. Cell size (area, 2D)
subplot(2,3,4);
errorbar(cell_area, eta_all, eta_all_err, 'ko');
xlabel('Cell area (\mum^2)');
ylabel('Viscosity (P)');
ylim([0 15]);
    
% Plot viscosity vs. Cap size proportion
subplot(2,3,5);
errorbar(cap_proportion, eta_all, eta_all_err, 'ko');
xlabel('Cap proportion of cell');
ylabel('Viscosity (P)');
ylim([0 15]);
        
% Plot viscosity vs. Initial cell flour. intensity (concentration)
subplot(2,3,6);
errorbar(fluor_cell_conc_ini, eta_all, eta_all_err, 'ko');
xlabel('Initial fluor. intensity');
ylabel('Viscosity (P)');
ylim([0 15]);
    
    
    
    
    
    
    
    
    
    

